# Set the current script directory
$SCRIPT_DIR = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Set-Location -Path $SCRIPT_DIR

# Set the root directory, plugin directory, media directory, and log directory
$ROOT_DIR = Split-Path -Path (Split-Path -Path $SCRIPT_DIR -Parent) -Parent
$PLUGIN_DIR = Join-Path $ROOT_DIR "plugins"
$MEDIA_DIR = Join-Path $ROOT_DIR "media"
$LOG_DIR = Join-Path $ROOT_DIR "logs"

# Create directories if they do not exist
if (-Not (Test-Path -Path $PLUGIN_DIR)) {
    New-Item -ItemType Directory -Path $PLUGIN_DIR
}
if (-Not (Test-Path -Path $MEDIA_DIR)) {
    New-Item -ItemType Directory -Path $MEDIA_DIR
}
if (-Not (Test-Path -Path $LOG_DIR)) {
    New-Item -ItemType Directory -Path $LOG_DIR
}

# Set npm registry and install packages
npm config set registry https://registry.npmmirror.com
npm install -g pnpm@v8.3.1
npm install -g pm2
pnpm config set registry https://registry.npmmirror.com

# Run the backend and frontend scripts
& "$SCRIPT_DIR\wex-backend.ps1"
& "$SCRIPT_DIR\wex-deploy-worker.ps1"
& "$SCRIPT_DIR\wex-frontend.ps1"
